package cn.itcast.config;


import cn.itcast.interceptor.MyInterceptor;
import cn.itcast.interceptor.TokenInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @Author: 张松
 * @Description:
 * @Date: Created in 18:16 2020/7/2
 * @Modified By:
 */
@Configuration
@EnableSwagger2
@ConditionalOnProperty(prefix = "swagger2",value = {"enable"},havingValue = "true")
public class Swagger2Config extends WebMvcConfigurationSupport{

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .pathMapping("/")
                .select()
                //为当前包下的controller生成api文档 cn.web.web_module cn.itcast.web cn.web.web_module cn.horesman.web
                .apis(RequestHandlerSelectors.basePackage("cn.merchant.web"))
                //过滤什么路径
                .paths(PathSelectors.any())
                .build().apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        //设置文档信息
        return new ApiInfoBuilder()
                .title("大黄牛")
                .description("大黄牛智能回收接口文档")
                .version("1.0")
                .contact(new Contact("xiaosong", "www.baidu.com", "578854242@qq.com"))
                .license("The Apache License")
                .licenseUrl("http://www.baidu.com")
                .build();
    }

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor()).addPathPatterns("/wxshop/test/**").excludePathPatterns("/wxshop/user/**","/wxshop/user/imageUpload");
        registry.addInterceptor(new TokenInterceptor()).addPathPatterns("/wxshop/message/**","/dhn/message/**").excludePathPatterns("/wxshop/message/login","/wxshop/message/sendOrder","/wxshop/message/getAllHorseman","/wxshop/message/check","/dhn/message/addchildrenClassify","/dhn/message/addMainClassify");
        super.addInterceptors(registry);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry
                .addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry
                .addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}
